<template>
  <div class="cate-container" :style="{ height:wh + 'px' }">
    <div class="left">
      <van-sidebar v-model="activeKey">
        <van-sidebar-item
          @click="clickHandler(index)"
          v-for="(item,index) in catesList"
          :key="index"
          :title="item.cat_name"
        />
      </van-sidebar>
    </div>
    <div class="right">
      <!-- 循环二级分类 -->
      <van-row v-for="(item,index) in rightBoxData" :key="index">
        <h5>{{ item.cat_name }}</h5>
        <!-- 循环三级分类 -->
        <van-row gutter="20">
          <div
            @click="$router.push('/pages/goods_list?cid=' + item2.cat_id)"
            v-for="(item2,index2) in item.children"
            :key="index2"
          >
            <van-col span="8">
              <img :src="item2.cat_icon" alt />
              <div>{{ item2.cat_name }}</div>
            </van-col>
          </div>
        </van-row>
      </van-row>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      wh: 0,
      activeKey: 0,
      catesList: [],
      rightBoxData: []
    };
  },
  created() {
    this.getCatesList();
  },
  mounted() {
    // 计算设备的高度
    this.wh = document.documentElement.clientHeight - 46;
    console.log(this.wh);
  },
  methods: {
    async getCatesList() {
      const { data: res } = await this.$http.get("/categories");
      if (res.meta.status !== 200) {
        return this.$toast.fail("获取分类数据失败");
      }
      // 返回的是分类的总数据
      // 数据结构 一级分类--->children属性 是二级分类 -----> children属性 三级分类
      this.catesList = res.message;
      this.rightBoxData = this.catesList[0].children;
    },
    clickHandler(index) {
      this.rightBoxData = this.catesList[index].children;
    }
  }
};
</script>

<style lang="less" scoped>
.cate-container {
  display: flex;
  box-sizing: border-box;
  padding-bottom: 50px;
  .left {
    overflow-y: auto;
    width: 80px;
  }
  .right {
    flex: 1;
    overflow-y: auto;
    text-align: center;
    h5 {
      font-size: 16px;
    }
    .van-col {
      height: 80px;
      img {
        width: 60px;
        height: 60px;
      }
      div {
        font-size: 12px;
      }
    }
  }
}
</style>